package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.ticket.domain.PriceTicket;
import cn.wolfcode.luowowo.ticket.domain.Scenic;
import cn.wolfcode.luowowo.ticket.query.QueryScenic;
import cn.wolfcode.luowowo.ticket.service.IPaymentService;
import cn.wolfcode.luowowo.ticket.service.IPriceTicketService;
import cn.wolfcode.luowowo.ticket.service.IScenicService;
import cn.wolfcode.luowowo.util.AjaxResult;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("ticket")
public class PriceController {
    @Reference
    private IPriceTicketService priceTicketService;

    @Reference
    private IScenicService scenicService;

    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IPaymentService paymentService;

    @Reference
    private IDestinationService destinationService;

    /**
     * 查询符合当日购买的门票,取前四个
     */
    //主页面
    @RequestMapping("/index")
    //进行登录拦截,判断用户是否登录
    @RequiredLogin
    public String index(Model model) {
        List<PriceTicket> priceTicketList = priceTicketService.selectStateAll();
        List<PriceTicket> tickets = priceTicketService.selectAll();
        List<Scenic> scenicList = scenicService.selectAll();
        List<Destination> destinationList = destinationService.listTop8();
        model.addAttribute("destinationList", destinationList);
        model.addAttribute("tickets", tickets);
        model.addAttribute("priceTicketList", priceTicketList);
        model.addAttribute("scenicList", scenicList);
        return "ticket/index";
    }

    //选购页面
    @RequestMapping("/detail")
    public String detail(Model model, Long tid, QueryScenic qo) {
        //通过主页面点击,返回点击票价的全部数据
        PriceTicket ticket = priceTicketService.queryScenicId(tid);
        model.addAttribute("ticket", ticket);
        //查询全部数据然后补充到票价详情页面
        List<PriceTicket> priceTicketList = priceTicketService.selectAll();
        model.addAttribute("priceTicketList", priceTicketList);
        //补充页面观关景点页面
        List<PriceTicket> QueryScorerSightseeingList = priceTicketService.QueryScorerSightseeing(qo);
        model.addAttribute("QueryScorerSightseeingList", QueryScorerSightseeingList);
        //补充页面好评景点
        List<PriceTicket> QueryScorerGoodList = priceTicketService.QueryScorerGood(qo);
        model.addAttribute("QueryScorerGoodList", QueryScorerGoodList);
        return "ticket/detail";
    }

    //支付页面
    @RequestMapping("/addOrder")
    public String addOrder(Model model, Long tid, @UserParam UserInfo user) {
        PriceTicket ticket = priceTicketService.queryScenicId(tid);
        model.addAttribute("ticket", ticket);
        return "ticket/addOrder";
    }

    //验证码
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public AjaxResult sendVerifyCode(String phone) {
        try {
            userInfoService.sendVerifyCode(phone);
            return AjaxResult.SUCCESS;
        } catch (LogicException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    //付款
    @RequestMapping("/submitCreateOrder")
    @ResponseBody
    public AjaxResult submitCreateOrder(Model model, Long ticketId, Integer num, String msgCode, @UserParam UserInfo user, @DateTimeFormat(pattern = "yyyy-MM-dd") Date showVisitTimeInput) {
        try {
            paymentService.insetSubmitCreateOrder(ticketId, num, user, msgCode, showVisitTimeInput);
            return AjaxResult.SUCCESS;
        } catch (LogicException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }
}
